<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\Operation;

use App\Models\AmazonAds\AdsError;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
class SyncAdsError extends BatchAction
{
    public $name = '批量处理';

    public function handle(Collection $collection)
    {
        try{
            $count = 0;
            foreach ($collection as $model) {
                DB::beginTransaction();
                if($model->status == AdsError::STATUS_NO){
                    DB::rollBack();
                    continue;
                }

                $model->status = AdsError::STATUS_YES; //已处理
                $flag = $model->save();
                if(!$flag){
                    DB::rollBack();
                }else{
                    $count++;
                    DB::commit();
                }
            }
            return $this->response()->success("成功处理{$count}条记录！")->refresh();
        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }
}
